package com.floreantpos.util;

import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;

import com.floreantpos.model.MenuCategory;
import com.floreantpos.model.MenuGroup;
import com.floreantpos.model.MenuItem;
import com.floreantpos.model.MenuModifier;
import com.floreantpos.model.MenuModifierGroup;
import com.floreantpos.model.Restaurant;
import com.floreantpos.model.Shift;
import com.floreantpos.model.Tax;
import com.floreantpos.model.User;
import com.floreantpos.model.UserPermission;
import com.floreantpos.model.UserType;
import com.floreantpos.model.dao.MenuCategoryDAO;
import com.floreantpos.model.dao.MenuGroupDAO;
import com.floreantpos.model.dao.MenuItemDAO;
import com.floreantpos.model.dao.MenuModifierDAO;
import com.floreantpos.model.dao.ModifierGroupDAO;
import com.floreantpos.model.dao.RestaurantDAO;
import com.floreantpos.model.dao.ShiftDAO;
import com.floreantpos.model.dao.TaxDAO;
import com.floreantpos.model.dao.UserDAO;
import com.floreantpos.model.dao.UserTypeDAO;
import com.floreantpos.model.dao._RootDAO;

public class DbInit {

  public static void main(String[] args) throws Exception {
    _RootDAO.dbCleanerInitialize();

    Restaurant restaurant = new Restaurant();
    restaurant.setId(Integer.valueOf(1));
    restaurant.setName("Floreo");
    restaurant.setAddressLine1("Sistema para Gestão");
    restaurant.setAddressLine2("de Clientes");
    restaurant.setAddressLine3("no Ponto de Venda");
    restaurant.setTelephone("(12) 3456-7890");
    restaurant.setTestSystem(Boolean.TRUE);
    RestaurantDAO.getInstance().saveOrUpdate(restaurant);

    Tax tax = new Tax();
    tax.setName("BR");
    tax.setRate(Double.valueOf(0));
    TaxDAO.getInstance().saveOrUpdate(tax);

    Shift shift = new Shift();
    shift.setName(com.floreantpos.POSConstants.GENERAL);
    Date shiftStartTime = ShiftUtil.buildShiftStartTime(0, 0, 0, 11, 59, 1);
    Date shiftEndTime = ShiftUtil.buildShiftEndTime(0, 0, 0, 11, 59, 1);

    shift.setStartTime(shiftStartTime);
    shift.setEndTime(shiftEndTime);
    long length = Math.abs(shiftStartTime.getTime() - shiftEndTime.getTime());

    shift.setShiftLength(Long.valueOf(length));
    ShiftDAO.getInstance().saveOrUpdate(shift);

    UserType type = new UserType();
    type.setName(com.floreantpos.POSConstants.ADMINISTRATOR);
    type.setPermissions(new HashSet<UserPermission>(Arrays.asList(UserPermission.permissions)));
    UserTypeDAO.getInstance().saveOrUpdate(type);

    User u = new User();
    u.setUserId(123);
    u.setSsn("123");
    u.setPassword("123");
    u.setFirstName("Atendente");
    u.setLastName("Floreo");
    u.setNewUserType(type);

    UserDAO dao = new UserDAO();
    dao.saveOrUpdate(u);

    MenuCategory category = new MenuCategory();
    category.setName("Bebidas");
    category.setBeverage(Boolean.TRUE);
    category.setVisible(Boolean.TRUE);
    MenuCategoryDAO.getInstance().saveOrUpdate(category);

    MenuCategory category1 = new MenuCategory();
    category1.setName("Lanches");
    category1.setBeverage(Boolean.FALSE);
    category1.setVisible(Boolean.TRUE);
    MenuCategoryDAO.getInstance().saveOrUpdate(category1);
    
    MenuCategory category2 = new MenuCategory();
    category2.setName("Pizzas");
    category2.setBeverage(Boolean.FALSE);
    category2.setVisible(Boolean.TRUE);
    MenuCategoryDAO.getInstance().saveOrUpdate(category2); 
    
    MenuCategory category3 = new MenuCategory();
    category3.setName("Refeições");
    category3.setBeverage(Boolean.FALSE);
    category3.setVisible(Boolean.TRUE);
    MenuCategoryDAO.getInstance().saveOrUpdate(category3);    

    MenuGroup group1 = new MenuGroup();
    group1.setParent(category);
    group1.setName("Bebidas Geladas");
    group1.setVisible(Boolean.TRUE);
    MenuGroupDAO.getInstance().save(group1);

    MenuGroup group2 = new MenuGroup();
    group2.setParent(category1);
    group2.setName("Favoritos");
    group2.setVisible(Boolean.TRUE);
    MenuGroupDAO.getInstance().save(group2);

    MenuGroup group3 = new MenuGroup();
    group3.setParent(category2);
    group3.setName("Especialidades");
    group3.setVisible(Boolean.TRUE);
    MenuGroupDAO.getInstance().save(group3);
    
    MenuGroup group4 = new MenuGroup();
    group4.setParent(category3);
    group4.setName("Buffet");
    group4.setVisible(Boolean.TRUE);
    MenuGroupDAO.getInstance().save(group4);    
    
    MenuModifierGroup modifierGroup = new MenuModifierGroup();
    modifierGroup.setName("Hamburguer");
    ModifierGroupDAO.getInstance().save(modifierGroup);

    MenuModifier modifier1 = new MenuModifier();
    modifier1.setName("Queijo");
    modifier1.setModifierGroup(modifierGroup);
    modifier1.setPrice(1.0);
    modifier1.setExtraPrice(1.0);
    modifier1.setShouldPrintToKitchen(Boolean.TRUE);
    modifier1.setPizzaModifier(Boolean.FALSE);
    modifier1.setTax(tax);
    MenuModifierDAO.getInstance().save(modifier1);

    MenuModifier modifier2 = new MenuModifier();
    modifier2.setName("Bacon");
    modifier2.setModifierGroup(modifierGroup);
    modifier2.setPrice(1.0);
    modifier2.setExtraPrice(1.0);
    modifier2.setShouldPrintToKitchen(Boolean.TRUE);
    modifier2.setPizzaModifier(Boolean.FALSE);
    modifier2.setTax(tax);
    MenuModifierDAO.getInstance().save(modifier2);
    
    MenuModifierGroup modifierGroup1 = new MenuModifierGroup();
    modifierGroup1.setName("Pizzas");
    ModifierGroupDAO.getInstance().save(modifierGroup1); 
    
    MenuModifier modifier3 = new MenuModifier();
    modifier3.setName("Mussarela");
    modifier3.setModifierGroup(modifierGroup1);
    modifier3.setPrice(12.0);
    modifier3.setExtraPrice(12.0);
    modifier3.setShouldPrintToKitchen(Boolean.TRUE);
    modifier3.setPizzaModifier(Boolean.TRUE);
    modifier3.setTax(tax);
    MenuModifierDAO.getInstance().save(modifier3);

    MenuModifier modifier4 = new MenuModifier();
    modifier4.setName("Portuguesa");
    modifier4.setModifierGroup(modifierGroup1);
    modifier4.setPrice(14.0);
    modifier4.setExtraPrice(14.0);
    modifier4.setShouldPrintToKitchen(Boolean.TRUE);
    modifier4.setPizzaModifier(Boolean.TRUE);
    modifier4.setTax(tax);
    MenuModifierDAO.getInstance().save(modifier4);    

    MenuItem item1 = new MenuItem();
    item1.setParent(group1);
    item1.setName("Coca-Cola");
    item1.setPrice(Double.valueOf(2.0));
    item1.setTax(tax);
    item1.setShouldPrintToKitchen(Boolean.TRUE);
    item1.setVisible(Boolean.TRUE);
    item1.setBuffet(Boolean.FALSE);
    MenuItemDAO.getInstance().save(item1);

    MenuItem item2 = new MenuItem();
    item2.setParent(group2);
    item2.setName("Hamburguer");
    item2.setPrice(Double.valueOf(2.0));
    item2.setTax(tax);
    item2.setShouldPrintToKitchen(Boolean.TRUE);
    item2.setVisible(Boolean.TRUE);
    item2.setBuffet(Boolean.FALSE);
    MenuItemDAO.getInstance().save(item2);

    MenuItem item3 = new MenuItem();
    item3.setParent(group2);
    item3.setName("Misto Quente");
    item3.setPrice(Double.valueOf(2.0));
    item3.setTax(tax);
    item3.setShouldPrintToKitchen(Boolean.TRUE);
    item3.setVisible(Boolean.TRUE);
    item3.setBuffet(Boolean.FALSE);
    MenuItemDAO.getInstance().save(item3);
    
    MenuItem item4 = new MenuItem();
    item4.setParent(group3);
    item4.setName("Pizza");
    item4.setPrice(Double.valueOf(2.0));
    item4.setTax(tax);
    item4.setShouldPrintToKitchen(Boolean.TRUE);
    item4.setVisible(Boolean.TRUE);
    item4.setBuffet(Boolean.FALSE);
    MenuItemDAO.getInstance().save(item4); 
    
    MenuItem item5 = new MenuItem();
    item5.setParent(group4);
    item5.setName("Almoço por Kilo");
    item5.setPrice(Double.valueOf(12.0));
    item5.setTax(tax);
    item5.setShouldPrintToKitchen(Boolean.FALSE);
    item5.setVisible(Boolean.TRUE);
    item5.setBuffet(Boolean.TRUE);
    MenuItemDAO.getInstance().save(item5);    

    System.exit(0);
  }
}
